"""
日程管理相关的 Pydantic 模型
"""

from datetime import datetime

from pydantic import BaseModel, Field

from src.models.schedule_model import (
    SchedulePriority,
    ScheduleRepeatType,
    ScheduleStatus,
)
from src.schemas import Page, PageResult


class ScheduleBase(BaseModel):
    """日程基础模型"""

    title: str = Field(..., min_length=1, max_length=200, description="日程标题")
    description: str | None = Field(None, max_length=2000, description="日程描述")
    start_time: datetime = Field(..., description="开始时间")
    end_time: datetime = Field(..., description="结束时间")
    status: ScheduleStatus = Field(ScheduleStatus.PENDING, description="日程状态")
    priority: SchedulePriority = Field(SchedulePriority.MEDIUM, description="优先级")
    repeat_type: ScheduleRepeatType = Field(
        ScheduleRepeatType.NONE, description="重复类型"
    )
    repeat_until: datetime | None = Field(None, description="重复截止日期")
    is_reminder_enabled: bool = Field(False, description="是否启用提醒")
    reminder_minutes: int | None = Field(
        None, ge=0, le=10080, description="提前提醒分钟数"
    )


class ScheduleCreate(ScheduleBase):
    """创建日程模型"""

    participant_ids: list[int] | None = Field(None, description="参与者ID列表")


class ScheduleSearch(Page):
    """日程检索参数"""

    start_date: datetime | None = Field(default=None, description="开始日期")
    end_date: datetime | None = Field(default=None, description="结束日期")
    status: ScheduleStatus | None = Field(default=None, description="状态过滤")
    priority: SchedulePriority | None = Field(default=None, description="优先级过滤")
    keyword: str | None = Field(default=None, description="搜索关键词")


class ScheduleUpdate(BaseModel):
    """更新日程模型"""

    # title字段不允许更新,因为与参与者记录有关联关系
    description: str | None = Field(None, max_length=2000, description="日程描述")
    start_time: datetime | None = Field(None, description="开始时间")
    end_time: datetime | None = Field(None, description="结束时间")
    status: ScheduleStatus | None = Field(None, description="日程状态")
    priority: SchedulePriority | None = Field(None, description="优先级")
    repeat_type: ScheduleRepeatType | None = Field(None, description="重复类型")
    repeat_until: datetime | None = Field(None, description="重复截止日期")
    is_reminder_enabled: bool | None = Field(None, description="是否启用提醒")
    reminder_minutes: int | None = Field(
        None, ge=0, le=10080, description="提前提醒分钟数"
    )
    location: str | None = Field(None, max_length=500, description="位置信息")
    notes: str | None = Field(None, max_length=2000, description="备注信息")
    participant_ids: list[int] | None = Field(None, description="参与者ID列表")


class ScheduleResponse(ScheduleBase):
    """日程响应模型"""

    id: int
    family_id: int
    family_name: str | None = None
    created_at: datetime
    updated_at: datetime | None = None

    model_config = {"from_attributes": True}


class ScheduleParticipantResponse(BaseModel):
    """日程参与者响应模型"""

    id: int
    family_id: int
    family_name: str | None = None
    schedule_id: int
    member_id: int
    member_name: str | None = None
    is_required: bool
    has_confirmed: bool
    confirmed_at: datetime | None = None
    notes: str | None = None

    model_config = {"from_attributes": True}


class ScheduleDetailResponse(ScheduleResponse):
    """日程详情响应模型"""

    participants: list[ScheduleParticipantResponse] = []

    model_config = {"from_attributes": True}


class ScheduleList(PageResult):
    """日程列表响应模型"""

    schedules: list[ScheduleResponse]


class ScheduleStats(BaseModel):
    """日程统计信息模型"""

    total_count: int
    pending_count: int
    in_progress_count: int
    completed_count: int
    cancelled_count: int
    high_priority_count: int
    urgent_count: int
